package com.example.mymvp_2.Base;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.werb.permissionschecker.PermissionChecker;

public abstract class BaseActivity<P extends BasePressenter> extends AppCompatActivity implements BaseView {
    protected P mPresenter;
    protected PermissionChecker permissionChecker;
    protected boolean hasPermission;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        permissionChecker= new PermissionChecker(this);
        if (!(hasPermission=!permissionChecker.isLackPermissions(PERMISSIONS))){
            ShowAlertWhyGetPermissions("本应用需要您授予相关权限才可使用本程序的一些功能，如果没有允许的话那么可能会影响到您的app使用，您放心，我们会保证您的隐私，不会滥用权限，具体权限使用情况，请查看官网www.baidu.com");
        }else {
        }
        initView();
        initData();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        hasPermission=!permissionChecker.isLackPermissions(PERMISSIONS);
        if (hasPermission) {
        }
    }

    public void ShowAlertWhyGetPermissions(String msg){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("权限申请")
                .setMessage(msg)
                .setPositiveButton("同意", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        getPermission();
                    }
                })
                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setCancelable(false)
                .show()
        ;

    }


    //所需的权限
    final String[] PERMISSIONS = new String[]{
            //SDK在Android 6.0下需要进行运行检测的权限如下：
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE
    };
    public  void getPermission(){
        if (permissionChecker.isLackPermissions(PERMISSIONS)) {//先判断有没有权限 没有权限弹出提示让你选择
            permissionChecker.requestPermissions();
        }else {
            //有权限后的操作
        }

    };

    public abstract int getLayout();
    public abstract void initView();
    public abstract void initData();

}
